package com.example.config;

import io.swagger.v3.oas.annotations.Operation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(true)   // 使用使用 swagger 开关。默认 true ，可省略。
                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.example.web.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())     // 过滤器：对外暴露所有 uri
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXX 项目接口文挡") //  可以用来自定义API的主标题
                .description("XXX Project Swagger3 UserService Interface") // 可以用来描述整体的API
//                .termsOfServiceUrl("http://localhost:8080/swagger-ui/index.html") // 用于定义服务的域名
                .version("1.0") // 可以用来定义版本。
                .build();
    }

}
